/*
 * Thread.h
 *
 *  Created on: 2009/12/09
 *      Author: Peter.Song
 */

#ifndef THREAD_H_
#define THREAD_H_

#include "MemoryPool.h"

#ifdef WIN32

#include <windows.h>
#include <process.h>
typedef HANDLE thread_primative;
extern __declspec( thread ) CLocalMempool* TH_LOCAL_MEMPOOL_PTR;
#define THREAD_ENTRY_FUNC(ENTRY_NAME) unsigned __stdcall ENTRY_NAME(void* param)

#else

#include <pthread.h>
typedef pthread_t thread_primative;
extern __thread CLocalMempool* TH_LOCAL_MEMPOOL_PTR;
#define THREAD_ENTRY_FUNC(ENTRY_NAME) void* ENTRY_NAME(void* param)

#endif

class CRunnable{
public:
	CRunnable(){}
	virtual ~CRunnable(){}
	virtual void Run() = 0;
};

class CXThread{
public:
	CXThread();
	CXThread(CRunnable* target);
	static void Initalize();
	static void Finalize();
	void Start();
	void Join();
	thread_primative GetThreadId() { return m_thread; }
	static THREAD_ENTRY_FUNC(thread_entry);
	virtual ~CXThread();
	void SetRunnableTarget(CRunnable* target);
protected:
	virtual void run();
private:
	thread_primative m_thread;
	CRunnable* m_target;
};


#endif /* THREAD_H_ */
